// 申请设备 + 绑定门店 + 上报设备参数

const iotApply =  require("./iotApply")
const iotBind =  require("./iotBind")
const iotClient =  require("./iotClient")
const par = require("./param")


const deviceNum = 1
const bindUsername = 'huangshuo1'
const bindUserUid = 'huangshuo1'
const bindPassword = 'Ut123456'
const bindMerchantId = '684647326388314112'
const bindStoreId = '712100416640806912'
const bindMerchantName = "huangshuo1商家"
const bindStoreName = "huangshuo1店铺3"

async function main() {
    deviceList = await iotApply.applyIotDevice(deviceNum)
    await iotBind.loginAndBind({
        username: bindUsername,
        password: bindPassword,
        merchantId: bindMerchantId,
        storeId: bindStoreId,
        devices: deviceList
    })

    await iotClient.connect(deviceList)
    for (device of deviceList) {
        let client = iotClient.deviceClients[device.dn]
        for (let parlist of [par.getBaseParam(), par.getSeasonPrarm(), par.getRemindPrarm(), par.getWokUpkeepSettingParam(), par.getIngrUpkeepSettingParam(), par.getOtherSettingParam()]) {
            let dataArray = []
            for (const param of parlist) {
                dataArray.push({
                    eventName:  "deviceParamUpdate",
                    data: {
                        "content":{
                            "cgid": device.cgid,
                            "deviceAlias": device.deviceAlias,
                            "pk": device.pk,
                            "productName": device.pn,
                            "merchantId": bindMerchantId,
                            "merchantName": bindMerchantName,
                            "storeId": bindStoreId,
                            "storeName": bindStoreName,
                            // "paramTmplGroupId": 0,
                            // "paramTmplId": 0,
                            "settingWay": 0,
                            "updateUserUuid": bindUserUid,
                            "updateTime": Date.now(),
                            ...param,
                        }
                    }
                })
            }
            client.postEvent('generalEvent', body = {dataArray: dataArray}, (res)=>{
                console.log(client.model.deviceName + "：上报设备参数")
            });
        }
    }
}

main()